package top.jsls9.controller;

import org.jsoup.Connection;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import top.jsls9.util.HttpUtils;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**一个简单的推送
 * @author bSu
 * @date 2020/7/9 - 17:26
 */
@Component
public class NewBookController {

    private int postId=123;

    @Scheduled(cron = "0 0 0/1 * * ? ")//每小时执行一次
    //@Scheduled(cron = "0/1 * 0/1 * * ?") 每秒执行一次
    public void newBook() throws IOException {

        Connection.Response response = HttpUtils.get("http://www.xbiquge.la/15/15021/");
        //System.out.println(response.body());

        String s=response.body();
        //System.out.println(s);
        String pre="最新章节：<a href=\"http://www.xbiquge.la/15/15021/";
        String suf=".html\">第";
        int newPostId = Integer.valueOf(s.substring((s.indexOf(pre) + pre.length()), s.indexOf(suf)));
        System.out.println("获取到的最新章节："+newPostId);
        if(postId<newPostId){//最新章节
            postId=newPostId;
            System.out.println("来了来了");
            //开始推送准备
            //1.获取最新标题
            //pre="/25329832.html' >";//这个测试之后忘记改了，导致每次都按照固定的章节开始截取
            pre="/"+newPostId+".html' >";//这样才对嘛，获取最新的章节名推送更新通知即可。
            suf="</dl>";
            String title =s.substring((s.indexOf(pre) + pre.length()), s.indexOf(suf));
            //1.1去掉多余的标题
            title=title.replace("</a></dd>","");
            System.out.println(title);
            //2获取正文内容
            //开启推送
            //String str="https://sc.ftqq.com/SCU79974Tac35eeae6ee447cb514dc6fbf952712b5e3bb7da0d9ea.send?text="+title+"&desp=电脑或者App打开无广告";
            Map<String,String> map=new HashMap<>();
            map.put("text",title);
            map.put("desp","http://www.xbiquge.la/15/15021/"+newPostId+".html"+"   PC网页和手机App无广告");
            HttpUtils.post("https://sc.ftqq.com/SCU79974Tac35eeae6ee447cb514dc6fbf952712b5e3bb7da0d9ea.send",map);

        }

    }


    //测试
    //@Scheduled(cron = "0/1 * 0/1 * * ?")
    public void test(){
        System.out.println(new SimpleDateFormat("yyyy-MM-dd hh-mm-ss").format(new Date()));
        String str="测试执行-->";
        str=str+new SimpleDateFormat("yyyy-MM-dd hh-mm-ss").format(new Date());
        System.out.println(str);
    }

    /*@Scheduled(cron="* * * * * ?")
    public void test1(){
        Random r = new Random();
        System.out.println(Thread.currentThread().getName() + " cron=* * * * * ? --- " + new Date());
        try{
            Thread.sleep(2000);
        }catch(Exception e){
            e.printStackTrace();
        }
    }
*/
}
